﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using portfolio.Common;
using portfolio.DataAccess.DancersDB;

namespace portfolio.Business
{
    public class DancersDBBL
    {
        public List<DancersDBData> GetAllSongs()
        {
            DancersDBDAO dancersDBDAO = new DancersDBDAO();
            return dancersDBDAO.GetAllSongs();
        }

        public List<DancersDBData> GetAllSongsForDances(List<string> danceIdentifier)
        {
            DancersDBDAO dancersDBDAO = new DancersDBDAO();
            return dancersDBDAO.GetAllSongsForDances(danceIdentifier);
        }

        public DancersDBData GetSongForSongID(int songID)
        {
            DancersDBDAO dancersDBDAO = new DancersDBDAO();
            return dancersDBDAO.GetSongForSongID(songID);
        }

        public List<DanceStyleData> GetAllDanceStyles()
        {
            DancersDBDAO dancersDBDAO = new DancersDBDAO();
            return dancersDBDAO.GetAllDanceStyles();
        }

        public List<DanceStyleData> GetAllDanceStylesForSong(int songID)
        {
            DancersDBDAO dancersDBDAO = new DancersDBDAO();
            return dancersDBDAO.GetAllDanceStylesForSong(songID);
        }

        public void InsertSong(DancersDBData dancersDBData)
        {
            DancersDBDAO dancersDBDAO = new DancersDBDAO();
            dancersDBDAO.InsertSong(dancersDBData);
        }

        public void UpdateSong(DancersDBData dancersDBData)
        {
            DancersDBDAO dancersDBDAO = new DancersDBDAO();
            dancersDBDAO.UpdateSong(dancersDBData);
        }

        public void DeleteSong(int songID)
        {
            DancersDBDAO dancersDBDAO = new DancersDBDAO();
            dancersDBDAO.DeleteSong(songID);
        }

        public string GetDancersDBScreenForValue(string value)
        {
            DancersDBDAO dancersDBDAO = new DancersDBDAO();
            return dancersDBDAO.GetDancersDBScreenForValue(value);
        }

        public bool Login(string username, string password)
        {
            DancersDBDAO dancersDBDAO = new DancersDBDAO();
            const string VIKey = "@1B2c3D4e5F6g7H8";
            string salt = "@38fmf9!!vofldkei" + username;
            byte[] plainTextBytes = Encoding.UTF8.GetBytes(password);

            byte[] keyBytes = new Rfc2898DeriveBytes(username, Encoding.ASCII.GetBytes(salt)).GetBytes(256 / 8);
            var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros };
            var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));

            byte[] cipherTextBytes;

            using (var memoryStream = new MemoryStream())
            {
                using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                {
                    cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                    cryptoStream.FlushFinalBlock();
                    cipherTextBytes = memoryStream.ToArray();
                    cryptoStream.Close();
                }
                memoryStream.Close();
            }
            return dancersDBDAO.Login(username,Convert.ToBase64String(cipherTextBytes));
        }
    }
}
